package com.bosintek.wordtest.gui;

import java.util.ArrayList;
import java.util.Vector;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.bosintek.wordtest.Bank;
import com.bosintek.wordtest.R;
import com.bosintek.wordtest.WordSet;
import com.bosintek.wordtest.util.DataManager;
import com.bosintek.wordtest.util.ObjectsManager;
import com.bosintek.wordtest.util.Parser;
import com.bosintek.wordtest.webAccess.Loader;

public class BanksActivity extends Activity implements OnClickListener {

	private Button btnSync;
	private Vector <Bank> banks;
	private BankAdapter bAdapter;
	private ArrayList<Bank> aListBanks;
	private ListView listBanks; 
	private Intent myIntent;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.banksact);
		this.btnSync = (Button) findViewById(R.id.btnSync);
		this.listBanks = (ListView) findViewById(R.id.listBanks);
		btnSync.setOnClickListener(this);
		aListBanks = new ArrayList<Bank>();
		bAdapter = new BankAdapter(this, R.layout.bankrow,aListBanks);
		this.listBanks.setAdapter(bAdapter);
		
		myIntent = this.getIntent();
		Bundle myExtras = myIntent.getExtras();
		
		/* MODIFICAR PARA SACAR VIEW DE BANCO...
		listContacts.setClickable(true);
		listContacts
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View v,
							int position, long arg3) {

						Intent myIntent = new Intent(v.getContext(),
								ProfileView.class);
						String profileEmail = ObjectsManager.getMyContacts().get(position).getEmail();//listItems.get(position);
						myIntent.putExtra("profileToChat", profileEmail);
						startActivityForResult(myIntent, 0);

					}
				});*/
		updateList();

	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.btnSync:
			//synchronize();
			synchroDEBUG();
			break;
		}
	}

	/**
	 * Local and remote Banks are synchronized.
	 */
	private void synchronize() {
		// load XML from the server
		// PROVISIONAL!! no cargo nada remoto...
		String remoteXML = Loader.getXML(ObjectsManager.getUser(),ObjectsManager.getPass());
		// Stored data handler.
		DataManager dm = new DataManager(ObjectsManager.getActivity());
		// Parse to Bank Vector
		Parser p = new Parser();
		// Local and remote banks are loaded in Vectors.
		Vector <Bank> remoteBanks = p.parseXML(remoteXML);
		Vector <Bank> localBanks = p.parseXML(dm.getBanksXML());
		// All available banks are compared in order to get only the newest.
		Vector <Bank> myBanks = bankComparison(localBanks,remoteBanks);
		// Update DataManager and ObjectManager
		dm.setBanksXML(banksXML(myBanks));
		ObjectsManager.setBanks(myBanks);
		// TODO: The result must be uploaded to the server in case there are any changes.
		updateList();
	}
	
	/**
	 * Local and remote Banks are synchronized.
	 */
	private void synchroDEBUG() {
		Loader loader = new Loader();
		// TODO: Read the filenames
		// TODO: Do this for each file
		Vector <WordSet> testWsets = loader.loadPlainBanks("load/000.txt");
		// Move the files to another location
		// TODO: Load each file as a different bank.
		
		//MIENTRAS TANTO:
		Bank temp = new Bank();
		temp.setName("Test bank");
		temp.setwSets(testWsets);
		Vector<Bank> myBanks = new Vector<Bank>();
		myBanks.add(temp);
		ObjectsManager.setBanks(myBanks);
		ObjectsManager.setCurrentBank(temp);
		
		// Stored data handler.
		DataManager dm = new DataManager(ObjectsManager.getActivity());
		// Update DataManager and ObjectManager
		dm.setBanksXML(banksXML(myBanks));
		ObjectsManager.setBanks(myBanks);
		// TODO: The result must be uploaded to the server in case there are any changes.
		updateList();
	}
	
	/**
	 * It returns the XML of a Banks Vector as a String.
	 * @return
	 */
	private String banksXML(Vector <Bank> myBanks){
		String value = "<banks>";
		int i = 0;
		while (i<myBanks.size()){
			value+=myBanks.get(i).toXML()+"\n";
			i++;
		}
		value+="</banks>";
		return value;
	}
	
	private Vector<Bank> bankComparison(Vector <Bank> localBanks, Vector <Bank> remoteBanks){
		//TODO: Compare bank IDs and return a Vector with unique IDs.
		// When two IDs match, return the newest.
		Vector<Bank> myBanks = new Vector<Bank>();
		myBanks.addAll(localBanks);
		myBanks.addAll(remoteBanks);
		return localBanks;
	}
	
	private void updateList() {
		this.aListBanks.clear();
		banks = ObjectsManager.getBanks();
		int i = 0;
		aListBanks.add(ObjectsManager.getMarked()); // Marked words will be shown as a special bank.
		while (i < banks.size()) {
			aListBanks.add(banks.get(i));
			i++;
		}
		this.bAdapter.notifyDataSetChanged();
	}

}